package com.igroupes.ovineherd.model.Style;

import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.util.DateUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class DateStyle extends BasicStyle {

    protected Boolean required;
    protected String message;
    protected String subType; //时间格式
    protected Boolean range = false; //范围
    protected String minDate = "";
    protected String maxDate = "";
    protected String inputFormat="YYYY-MM-DD";
    protected String format = "YYYY-MM-DD";
//    protected String format = "YYYY年MM月DD日 HH时mm分ss秒";

    protected DateStyle(String label, Object value) {
        super(label, value);
        this.message = "必传字段";
        this.required = false;
        this.type = FormKey.DATE.getCode();
        this.subType = "日期";
    }
    public DateStyle(String label, Object value, Boolean required) {
        super(label, value);
        this.message = "必传字段";
        this.type = FormKey.DATE.getCode();
        this.subType = "日期";
        this.required = required;
    }
    public DateStyle(String label, Object value, String type, Boolean range, Boolean required, String desc) {
        this(label, value, type);
        this.required = required;
        this.range = range;
        this.desc = desc;
    }
    protected DateStyle(String label, Object value, String subType) {
        super(label, value);
        this.message = "必传字段";
        this.required = false;
        this.type = FormKey.DATE.getCode();
        this.subType = subType;
    }

    private DateStyle(String label, Object value, Boolean required, String type) {
        this(label, value, type);
        this.required = required;
    }

    private DateStyle(String label, Object value, String desc, String type) {
        this(label, value, type);
        this.desc = desc;
    }

    public DateStyle(String label, Object value, Boolean required, String minDate, String maxDate) {
        this(label, value, required);
        this.minDate = minDate;
        this.maxDate = maxDate;
    }
    public DateStyle() {

    }

    @Override
    public String getDesc() {
        return desc;
    }

    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof DateStyle)){
            throw new ModelException("校验style类类型错误，date");
        }
        DateStyle style = (DateStyle) o;
        if(!style.getDisabled()){
            super.verify(o);
            if (!(style.getRequired() instanceof Boolean)){
                throw new ModelException("required格式错误");
            }
            String value = (String) style.getValue();
            if(style.getRequired() ){
                if(StringUtils.isBlank(value)){
                    throw new ModelException(style.getLabel()+"值是必传字段");
                }
            }
            if(StringUtils.isNotBlank(value)){
                if(!(style.getValue() instanceof String)){
                    throw new ModelException(style.getLabel()+"值必须是字符串");
                }

                if(!DateUtils.isValidDate(value, DateUtils.DATE_FORMAT)){
                    throw new ModelException(style.getLabel()+"时间格式不正确,yyyy-mm-dd");
                }
            }
        }
        return true;
    }
}
